package org.jeecg.demo.wx.service.impl;

import cn.hutool.core.date.DateUtil;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.demo.wx.dto.ModifyDelayDto;
import org.jeecg.demo.wx.entity.RlDelay;
import org.jeecg.demo.wx.entity.RlSchedule;
import org.jeecg.demo.wx.entity.RlTask;
import org.jeecg.demo.wx.enumeration.CompleteEnum;
import org.jeecg.demo.wx.enumeration.DelayEnum;
import org.jeecg.demo.wx.mapper.RlDelayMapper;
import org.jeecg.demo.wx.service.IRlDelayService;
import org.jeecg.demo.wx.service.IRlScheduleService;
import org.jeecg.demo.wx.service.IRlTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class RlDelayServiceImpl extends ServiceImpl<RlDelayMapper, RlDelay>
    implements IRlDelayService {

    @Autowired
    @Lazy
    private IRlTaskService rlTaskService;

    @Autowired
    @Lazy
    private IRlScheduleService rlScheduleService;

    @Override
    public void approveDelay(Integer scheduleId, String schedulerId) {
        RlTask task = rlTaskService.lambdaQuery()
                .in(RlTask::getRecipientTaskId, scheduleId)
                .one();
        rlScheduleService.lambdaUpdate()
                .eq(RlSchedule::getId, scheduleId)
                .set(RlSchedule::getIsComplete, CompleteEnum.DEFER.getCode())
                .update();
        this.save(new RlDelay()
                .setTitle(task.getTitle())
                .setScheduleId(scheduleId)
                .setSchedulerId(schedulerId)
                .setTaskId(task.getId())
                .setTaskerId(task.getUserId())
                .setState(DelayEnum.WAIT.getCode())
                .setStateMessage(DelayEnum.WAIT.getMsg()));
    }

    @Override
    public Long queryDelayNumber(String taskerId) {
        return this.lambdaQuery()
                .eq(RlDelay::getTaskerId, taskerId)
                .eq(RlDelay::getState, DelayEnum.WAIT.getCode())
                .count();
    }

    /**
     * 三个月内
     * @param schedulerId
     * @return
     */
    @Override
    public List<RlDelay> queryDelayList(String schedulerId) {
        return this.lambdaQuery()
                .eq(RlDelay::getSchedulerId, schedulerId)
                .le(RlDelay::getCreateTime, DateUtil.offsetMonth(DateUtil.date(), 3))
                .orderByDesc(RlDelay::getCreateTime)
                .list();
    }

    /**
     * 审批状态，0-待审批；10-同意；20-不同意
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void modifyState(ModifyDelayDto dto) {
        RlDelay delay = this.lambdaQuery().eq(RlDelay::getId, dto.getId()).one();
        // 同步两个表的数据
        if (dto.getState().equals(DelayEnum.FAIL.getCode())) {
            rlScheduleService.lambdaUpdate()
                    .eq(RlSchedule::getId, delay.getScheduleId())
                    .set(RlSchedule::getIsComplete, CompleteEnum.UNREAD.getCode())
                    .update();
            this.lambdaUpdate()
                    .eq(RlDelay::getId, dto.getId())
                    .set(RlDelay::getState, DelayEnum.FAIL.getCode())
                    .set(RlDelay::getStateMessage, DelayEnum.FAIL.getMsg())
                    .update();
        } else {
            rlScheduleService.lambdaUpdate()
                    .eq(RlSchedule::getId, delay.getScheduleId())
                    .set(RlSchedule::getIsComplete, CompleteEnum.UNREAD.getCode())
                    .set(RlSchedule::getEndTime, dto.getDateTime())
                    .update();
            this.lambdaUpdate()
                    .eq(RlDelay::getId, dto.getId())
                    .set(RlDelay::getState, dto.getState())
                    .set(RlDelay::getStateMessage, DelayEnum.SUCCESS.getMsg())
                    .update();
        }


    }

    @Override
    public List<RlDelay> queryApproveList(String taskerId) {
        return this.lambdaQuery()
                    .eq(RlDelay::getTaskerId, taskerId)
                    .eq(RlDelay::getState, DelayEnum.WAIT.getCode())
                    .list();
    }
}




